﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PreeRegisterModel;
using System.Data;
using PreeRegisterDAL;
using Newtonsoft.Json.Linq;

namespace PreeRegisterBLL
{
    public class LabReportBLL
    {

        public static LabReportListResponseModel GetLabReportListNew2(LabReportListRequestModel request)
        {
            LabReportListResponseModel labReportListResponseModel = new PreeRegisterModel.LabReportListResponseModel();
            try
            {
                //查询检验报告列表
                hars_inspectlist bll_inspectlist = new hars_inspectlist();
                DataSet ds = bll_inspectlist.GetList(9, request.cpatientcode);

                if (ds == null)
                {
                    labReportListResponseModel.code = "0";
                    labReportListResponseModel.msg = "获取检验报告列表异常";
                    return labReportListResponseModel;
                }

                //获取检验报告列表
                List<LabReportListItemResponseModel> LabReportListItemResponseModel_list = new List<LabReportListItemResponseModel>();

                foreach (DataRow drLabReport in ds.Tables[0].Rows)
                {
                    LabReportListItemResponseModel labReportListItemResponseModel = new LabReportListItemResponseModel();
                    labReportListItemResponseModel.ApplyNo = drLabReport["ApplyNo"].ToString();
                    labReportListItemResponseModel.cjgsj = drLabReport["cjgsj"].ToString();
                    labReportListItemResponseModel.MainID = drLabReport["MainID"].ToString();
                    labReportListItemResponseModel.OrderNo = drLabReport["OrderNo"].ToString();
                    labReportListItemResponseModel.ReportTypeName = drLabReport["ReportTypeName"].ToString();
                    labReportListItemResponseModel.TestSample = drLabReport["TestSample"].ToString();

                    LabReportListItemResponseModel_list.Add(labReportListItemResponseModel);
                }
                labReportListResponseModel.code = "1";
                labReportListResponseModel.msg = "获取检验报告列表成功";
                labReportListResponseModel.reportlist = LabReportListItemResponseModel_list;
                //返回
                return labReportListResponseModel;
            }
            catch (Exception err)
            {
                labReportListResponseModel.code = "0";
                labReportListResponseModel.msg = "获取检验报告列表异常";
                throw;
            }
        } 

        public static LabReportDetailResponseModel GetLabReportDetailNew2(LabReportDetailRequestModel request)
        {
            LabReportDetailResponseModel labReportDetailResponseModel = new LabReportDetailResponseModel();
            try
            {
                //查询检验报告明细
                hars_inspectlist bll_inspectlist = new hars_inspectlist();
                DataSet ds = bll_inspectlist.GetList(8, request.mainid);

                if (ds == null)
                {
                    labReportDetailResponseModel.code = "0";
                    labReportDetailResponseModel.msg = "获取检验报告明细异常";
                    return labReportDetailResponseModel;
                }

                //获取检验报告明细

                labReportDetailResponseModel.Age = ds.Tables[0].Rows[0]["Age"].ToString();
                labReportDetailResponseModel.BedNo = ds.Tables[0].Rows[0]["BedNo"].ToString();
                labReportDetailResponseModel.cbr = ds.Tables[0].Rows[0]["cbr"].ToString();
                labReportDetailResponseModel.cbsj = ds.Tables[0].Rows[0]["cbsj"].ToString();
                labReportDetailResponseModel.cjgsj = ds.Tables[0].Rows[0]["cjgsj"].ToString();
                labReportDetailResponseModel.Diagnose = ds.Tables[0].Rows[0]["Diagnose"].ToString();
                labReportDetailResponseModel.hdr = ds.Tables[0].Rows[0]["hdr"].ToString();
                labReportDetailResponseModel.jbsj = ds.Tables[0].Rows[0]["jbsj"].ToString();
                labReportDetailResponseModel.jyr = ds.Tables[0].Rows[0]["jyr"].ToString();
                labReportDetailResponseModel.jysj = ds.Tables[0].Rows[0]["jysj"].ToString();
                labReportDetailResponseModel.kdr = ds.Tables[0].Rows[0]["kdr"].ToString();
                labReportDetailResponseModel.kdsj = ds.Tables[0].Rows[0]["kdsj"].ToString();
                labReportDetailResponseModel.mainid = ds.Tables[0].Rows[0]["mainid"].ToString();
                labReportDetailResponseModel.MzZyNo = ds.Tables[0].Rows[0]["MzZyNo"].ToString();
                labReportDetailResponseModel.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                labReportDetailResponseModel.ReportFormat = ds.Tables[0].Rows[0]["ReportFormat"].ToString();
                labReportDetailResponseModel.ReportTypeName = ds.Tables[0].Rows[0]["ReportTypeName"].ToString();
                labReportDetailResponseModel.SampleStatus = ds.Tables[0].Rows[0]["SampleStatus"].ToString();
                labReportDetailResponseModel.SectionOffice = ds.Tables[0].Rows[0]["SectionOffice"].ToString();
                labReportDetailResponseModel.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                labReportDetailResponseModel.StandardSampleNo = ds.Tables[0].Rows[0]["StandardSampleNo"].ToString();
                labReportDetailResponseModel.TestSample = ds.Tables[0].Rows[0]["TestSample"].ToString();

                List<LabReportDetailItemResponseModel> LabReportDetailItemResponseModel_list = new List<LabReportDetailItemResponseModel>();

                hars_inspectdetails bll_inspectdetails = new hars_inspectdetails();
                DataSet ds_detail = bll_inspectdetails.GetList(9, request.mainid);
                if (ds == null)
                {
                    labReportDetailResponseModel.code = "0";
                    labReportDetailResponseModel.msg = "获取检验报告明细失败";
                    return labReportDetailResponseModel;
                }
                foreach (DataRow drlabReportDetail in ds_detail.Tables[0].Rows)
                {
                    LabReportDetailItemResponseModel labReportDetailItemResponseModel = new LabReportDetailItemResponseModel();

                    labReportDetailItemResponseModel.ConsultChar = drlabReportDetail["ConsultChar"].ToString();
                    labReportDetailItemResponseModel.mutualvalue = "";
                    labReportDetailItemResponseModel.TestItemChi = drlabReportDetail["TestItemChi"].ToString();
                    labReportDetailItemResponseModel.testmeans = drlabReportDetail["testmeans"].ToString();
                    labReportDetailItemResponseModel.TestResult = drlabReportDetail["TestResult"].ToString();
                    labReportDetailItemResponseModel.TestResultSign = drlabReportDetail["TestResultSign"].ToString();
                    labReportDetailItemResponseModel.Unit = drlabReportDetail["Unit"].ToString();

                    LabReportDetailItemResponseModel_list.Add(labReportDetailItemResponseModel);
                }

                labReportDetailResponseModel.detail = LabReportDetailItemResponseModel_list;
                labReportDetailResponseModel.code = "1";
                labReportDetailResponseModel.msg = "查询检验报告明细成功。";

                //返回
                return labReportDetailResponseModel;
            }
            catch (Exception err)
            {
                labReportDetailResponseModel.code = "0";
                labReportDetailResponseModel.msg = "获取检验报告明细异常";
                return labReportDetailResponseModel;

                throw;
            }
        }

        public static LabReportListResponseModel GetLabReportListNew(LabReportListRequestModel request)
        {
            //查询检验报告列表
            LabReportListResponseModel labReportListResponseModel = new LabReportListResponseModel();
            //获取检验报告列表
            List<LabReportListItemResponseModel> LabReportListItemResponseModel_list = new List<LabReportListItemResponseModel>();
            try
            {
                JObject obj_reportlist = ChangZhiHePingBLL.GetTestReportMainId("3", request.cpatientcode);//获取检验报告列表

                string code_reportlist = obj_reportlist["code"].ToString();
                if (string.Equals(code_reportlist, "1"))
                {
                    var mJObj_reportList = JArray.Parse(obj_reportlist["result"].ToString());//检验报告条目
                    foreach (var dr_reportList in mJObj_reportList)
                    {
                        LabReportListItemResponseModel labReportListItemResponseModel = new LabReportListItemResponseModel();
                        labReportListItemResponseModel.ApplyNo = dr_reportList["MainID"].ToString();
                        labReportListItemResponseModel.ReportTypeName = dr_reportList["ReportTypeName"].ToString();
                        labReportListItemResponseModel.TestSample = dr_reportList["yblx"].ToString();
                        labReportListItemResponseModel.OrderNo = "";
                        labReportListItemResponseModel.cjgsj = dr_reportList["cjgsj"].ToString().Replace("T"," ");
                        labReportListItemResponseModel.MainID = dr_reportList["MainID"].ToString();

                        LabReportListItemResponseModel_list.Add(labReportListItemResponseModel);
                    }
                    labReportListResponseModel.code = "1";
                    labReportListResponseModel.msg = "获取检验报告列表成功";
                    labReportListResponseModel.reportlist = LabReportListItemResponseModel_list;
                }
                else
                {
                    labReportListResponseModel.code = "0";
                    labReportListResponseModel.msg = "没有获取到检验报告，请核实。";
                    labReportListResponseModel.reportlist = LabReportListItemResponseModel_list;
                }
            }
            catch (Exception err)
            {
                labReportListResponseModel.code = "0";
                labReportListResponseModel.msg = "获取检验报告异常1：" + err.Message;
                labReportListResponseModel.reportlist = LabReportListItemResponseModel_list;
            }

            //返回
            return labReportListResponseModel;
        }

        public static LabReportDetailResponseModel GetLabReportDetailNew(LabReportDetailRequestModel request)
        {
            //查询检验报告明细
            LabReportDetailResponseModel labReportDetailResponseModel = new LabReportDetailResponseModel();
            try
            {
                JObject obj_reportdetail = ChangZhiHePingBLL.GetTestReportContent(request.mainid);//获取检验报告明细

                string code_reportdetail = obj_reportdetail["code"].ToString();
                if (string.Equals(code_reportdetail, "1"))
                {
                    var mJObj_reportdetail = JArray.Parse(obj_reportdetail["result"].ToString());
                    if (mJObj_reportdetail.ToString().Length > 3)
                    {
                        //labReportDetailResponseModel.Age = ds.Tables[0].Rows[0]["AgeUnit"].ToString();
                        //labReportDetailResponseModel.BedNo = ds.Tables[0].Rows[0]["BedNo"].ToString();
                        //labReportDetailResponseModel.cbr = ds.Tables[0].Rows[0]["cbr"].ToString();
                        //labReportDetailResponseModel.cbsj = ds.Tables[0].Rows[0]["cbsj"].ToString();
                        //labReportDetailResponseModel.cjgsj = ds.Tables[0].Rows[0]["cjgsj"].ToString();
                        //labReportDetailResponseModel.Diagnose = ds.Tables[0].Rows[0]["Diagnose"].ToString();
                        //labReportDetailResponseModel.hdr = ds.Tables[0].Rows[0]["hdr"].ToString();
                        //labReportDetailResponseModel.jbsj = ds.Tables[0].Rows[0]["jbsj"].ToString();
                        //labReportDetailResponseModel.jyr = ds.Tables[0].Rows[0]["jyr"].ToString();
                        //labReportDetailResponseModel.jysj = ds.Tables[0].Rows[0]["jysj"].ToString();
                        //labReportDetailResponseModel.kdr = ds.Tables[0].Rows[0]["kdr"].ToString();
                        //labReportDetailResponseModel.kdsj = ds.Tables[0].Rows[0]["kdsj"].ToString();
                        //labReportDetailResponseModel.mainid = ds.Tables[0].Rows[0]["mainid"].ToString();
                        //labReportDetailResponseModel.MzZyNo = ds.Tables[0].Rows[0]["MzZyNo"].ToString();
                        //labReportDetailResponseModel.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                        //labReportDetailResponseModel.ReportFormat = ds.Tables[0].Rows[0]["ReportFormat"].ToString();
                        //labReportDetailResponseModel.ReportTypeName = ds.Tables[0].Rows[0]["ReportTypeName"].ToString();
                        //labReportDetailResponseModel.SampleStatus = ds.Tables[0].Rows[0]["SampleStatus"].ToString();
                        //labReportDetailResponseModel.SectionOffice = ds.Tables[0].Rows[0]["SectionOffice"].ToString();
                        //labReportDetailResponseModel.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                        //labReportDetailResponseModel.StandardSampleNo = ds.Tables[0].Rows[0]["StandardSampleNo"].ToString();
                        //labReportDetailResponseModel.TestSample = ds.Tables[0].Rows[0]["TestSample"].ToString();

                        List<LabReportDetailItemResponseModel> LabReportDetailItemResponseModel_list = new List<LabReportDetailItemResponseModel>();
                        foreach (var dr_reportdetail in mJObj_reportdetail)
                        {
                            LabReportDetailItemResponseModel labReportDetailItemResponseModel = new LabReportDetailItemResponseModel();

                            labReportDetailItemResponseModel.ConsultChar = dr_reportdetail["ConsultChar"].ToString();
                            labReportDetailItemResponseModel.mutualvalue = dr_reportdetail["mutualvalue"].ToString();
                            labReportDetailItemResponseModel.TestItemChi = dr_reportdetail["TestItemChi"].ToString();
                            labReportDetailItemResponseModel.testmeans = dr_reportdetail["testmeans"].ToString();
                            labReportDetailItemResponseModel.TestResult = dr_reportdetail["TestResult"].ToString();
                            labReportDetailItemResponseModel.TestResultSign = dr_reportdetail["TestResultSign"].ToString();
                            labReportDetailItemResponseModel.Unit = dr_reportdetail["Unit"].ToString();

                            LabReportDetailItemResponseModel_list.Add(labReportDetailItemResponseModel);

                        }

                        //labReportDetailResponseModel.Age = ds.Tables[0].Rows[0]["AgeUnit"].ToString();
                        //labReportDetailResponseModel.BedNo = ds.Tables[0].Rows[0]["BedNo"].ToString();
                        //labReportDetailResponseModel.cbr = ds.Tables[0].Rows[0]["cbr"].ToString();
                        //labReportDetailResponseModel.cbsj = ds.Tables[0].Rows[0]["cbsj"].ToString();
                        //labReportDetailResponseModel.cjgsj = ds.Tables[0].Rows[0]["cjgsj"].ToString();
                        //labReportDetailResponseModel.Diagnose = ds.Tables[0].Rows[0]["Diagnose"].ToString();
                        //labReportDetailResponseModel.hdr = ds.Tables[0].Rows[0]["hdr"].ToString();
                        //labReportDetailResponseModel.jbsj = ds.Tables[0].Rows[0]["jbsj"].ToString();
                        //labReportDetailResponseModel.jyr = ds.Tables[0].Rows[0]["jyr"].ToString();
                        //labReportDetailResponseModel.jysj = ds.Tables[0].Rows[0]["jysj"].ToString();
                        //labReportDetailResponseModel.kdr = ds.Tables[0].Rows[0]["kdr"].ToString();
                        //labReportDetailResponseModel.kdsj = ds.Tables[0].Rows[0]["kdsj"].ToString();
                        //labReportDetailResponseModel.mainid = ds.Tables[0].Rows[0]["mainid"].ToString();
                        //labReportDetailResponseModel.MzZyNo = ds.Tables[0].Rows[0]["MzZyNo"].ToString();
                        //labReportDetailResponseModel.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                        //labReportDetailResponseModel.ReportFormat = ds.Tables[0].Rows[0]["ReportFormat"].ToString();
                        //labReportDetailResponseModel.ReportTypeName = ds.Tables[0].Rows[0]["ReportTypeName"].ToString();
                        //labReportDetailResponseModel.SampleStatus = ds.Tables[0].Rows[0]["SampleStatus"].ToString();
                        //labReportDetailResponseModel.SectionOffice = ds.Tables[0].Rows[0]["SectionOffice"].ToString();
                        //labReportDetailResponseModel.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                        //labReportDetailResponseModel.StandardSampleNo = ds.Tables[0].Rows[0]["StandardSampleNo"].ToString();
                        //labReportDetailResponseModel.TestSample = ds.Tables[0].Rows[0]["TestSample"].ToString();


                        //foreach (DataRow drlabReportDetail in ds.Tables[1].Rows)
                        //{
                        //    LabReportDetailItemResponseModel labReportDetailItemResponseModel = new LabReportDetailItemResponseModel();

                        //    labReportDetailItemResponseModel.ConsultChar = drlabReportDetail["ConsultChar"].ToString();
                        //    labReportDetailItemResponseModel.mutualvalue = drlabReportDetail["mutualvalue"].ToString();
                        //    labReportDetailItemResponseModel.TestItemChi = drlabReportDetail["TestItemChi"].ToString();
                        //    labReportDetailItemResponseModel.testmeans = drlabReportDetail["testmeans"].ToString();
                        //    labReportDetailItemResponseModel.TestResult = drlabReportDetail["TestResult"].ToString();
                        //    labReportDetailItemResponseModel.TestResultSign = drlabReportDetail["TestResultSign"].ToString();
                        //    labReportDetailItemResponseModel.Unit = drlabReportDetail["Unit"].ToString();

                        //    LabReportDetailItemResponseModel_list.Add(labReportDetailItemResponseModel);
                        //}

                        labReportDetailResponseModel.detail = LabReportDetailItemResponseModel_list;

                    }
                    else
                    {
                        labReportDetailResponseModel.code = "1";//code
                        labReportDetailResponseModel.msg = string.Format("获取检验报告成功,但数据异常：{0}，报告单号ID：{1}"
                            , mJObj_reportdetail.ToString(), request.mainid);//msg
                    }
                }
                else
                {
                    labReportDetailResponseModel.code = "1";//code
                    labReportDetailResponseModel.msg = string.Format("获取检验报告失败，报告单号ID：{0}"
                        , request.mainid);//msg
                }
            }
            catch (Exception err)
            {
                labReportDetailResponseModel.code = "1";//code
                labReportDetailResponseModel.msg = string.Format("获取检验报告失败，报告单号ID：{0}"
                    , request.mainid);//msg
            }
            //返回
            return labReportDetailResponseModel;
        }


        #region 卫宁
        /// <summary>
        /// 检验报告列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static LabReportListResponseModel GetLabReportList(LabReportListRequestModel request)
        {
            try
            {
                //查询检验报告列表
                LabReportListResponseModel labReportListResponseModel = new PreeRegisterModel.LabReportListResponseModel();

                DataSet ds = LabReportDAL.GetLabReportList(request.cpatientcode);

                //获取检验报告列表
                List<LabReportListItemResponseModel> LabReportListItemResponseModel_list = new List<LabReportListItemResponseModel>();

                foreach (DataRow drLabReport in ds.Tables[0].Rows)
                {
                    LabReportListItemResponseModel labReportListItemResponseModel = new LabReportListItemResponseModel();
                    labReportListItemResponseModel.ApplyNo = drLabReport["ApplyNo"].ToString();
                    labReportListItemResponseModel.cjgsj = drLabReport["cjgsj"].ToString();
                    labReportListItemResponseModel.MainID = drLabReport["MainID"].ToString();
                    labReportListItemResponseModel.OrderNo = drLabReport["OrderNo"].ToString();
                    labReportListItemResponseModel.ReportTypeName = drLabReport["ReportTypeName"].ToString();
                    labReportListItemResponseModel.TestSample = drLabReport["TestSample"].ToString();

                    LabReportListItemResponseModel_list.Add(labReportListItemResponseModel);
                }

                labReportListResponseModel.reportlist = LabReportListItemResponseModel_list;

                //返回
                return labReportListResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 检验报告明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static LabReportDetailResponseModel GetLabReportDetail(LabReportDetailRequestModel request)
        {
            try
            {
                //查询检验报告明细
                LabReportDetailResponseModel labReportDetailResponseModel = new PreeRegisterModel.LabReportDetailResponseModel();

                DataSet ds = LabReportDAL.GetLabReportDetail(request.mainid);

                if (ds == null)
                {
                    return null;
                }

                //获取检验报告明细

                labReportDetailResponseModel.Age = ds.Tables[0].Rows[0]["AgeUnit"].ToString();
                labReportDetailResponseModel.BedNo = ds.Tables[0].Rows[0]["BedNo"].ToString();
                labReportDetailResponseModel.cbr = ds.Tables[0].Rows[0]["cbr"].ToString();
                labReportDetailResponseModel.cbsj = ds.Tables[0].Rows[0]["cbsj"].ToString();
                labReportDetailResponseModel.cjgsj = ds.Tables[0].Rows[0]["cjgsj"].ToString();
                labReportDetailResponseModel.Diagnose = ds.Tables[0].Rows[0]["Diagnose"].ToString();
                labReportDetailResponseModel.hdr = ds.Tables[0].Rows[0]["hdr"].ToString();
                labReportDetailResponseModel.jbsj = ds.Tables[0].Rows[0]["jbsj"].ToString();
                labReportDetailResponseModel.jyr = ds.Tables[0].Rows[0]["jyr"].ToString();
                labReportDetailResponseModel.jysj = ds.Tables[0].Rows[0]["jysj"].ToString();
                labReportDetailResponseModel.kdr = ds.Tables[0].Rows[0]["kdr"].ToString();
                labReportDetailResponseModel.kdsj = ds.Tables[0].Rows[0]["kdsj"].ToString();
                labReportDetailResponseModel.mainid = ds.Tables[0].Rows[0]["mainid"].ToString();
                labReportDetailResponseModel.MzZyNo = ds.Tables[0].Rows[0]["MzZyNo"].ToString();
                labReportDetailResponseModel.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                labReportDetailResponseModel.ReportFormat = ds.Tables[0].Rows[0]["ReportFormat"].ToString();
                labReportDetailResponseModel.ReportTypeName = ds.Tables[0].Rows[0]["ReportTypeName"].ToString();
                labReportDetailResponseModel.SampleStatus = ds.Tables[0].Rows[0]["SampleStatus"].ToString();
                labReportDetailResponseModel.SectionOffice = ds.Tables[0].Rows[0]["SectionOffice"].ToString();
                labReportDetailResponseModel.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                labReportDetailResponseModel.StandardSampleNo = ds.Tables[0].Rows[0]["StandardSampleNo"].ToString();
                labReportDetailResponseModel.TestSample = ds.Tables[0].Rows[0]["TestSample"].ToString();

                List<LabReportDetailItemResponseModel> LabReportDetailItemResponseModel_list = new List<LabReportDetailItemResponseModel>();

                foreach (DataRow drlabReportDetail in ds.Tables[1].Rows)
                {
                    LabReportDetailItemResponseModel labReportDetailItemResponseModel = new LabReportDetailItemResponseModel();

                    labReportDetailItemResponseModel.ConsultChar = drlabReportDetail["ConsultChar"].ToString();
                    labReportDetailItemResponseModel.mutualvalue = drlabReportDetail["mutualvalue"].ToString();
                    labReportDetailItemResponseModel.TestItemChi = drlabReportDetail["TestItemChi"].ToString();
                    labReportDetailItemResponseModel.testmeans = drlabReportDetail["testmeans"].ToString();
                    labReportDetailItemResponseModel.TestResult = drlabReportDetail["TestResult"].ToString();
                    labReportDetailItemResponseModel.TestResultSign = drlabReportDetail["TestResultSign"].ToString();
                    labReportDetailItemResponseModel.Unit = drlabReportDetail["Unit"].ToString();

                    LabReportDetailItemResponseModel_list.Add(labReportDetailItemResponseModel);
                }

                labReportDetailResponseModel.detail = LabReportDetailItemResponseModel_list;

                //返回
                return labReportDetailResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }
        #endregion 
    }
}
